package com.woniuxy.processservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.processclient.client.CardFeignClient;
import com.woniuxy.processclient.client.OrderFeignClient;
import com.woniuxy.processclient.client.dto.OrderDto;
import com.woniuxy.processclient.client.dto.ValidCardDto;
import com.woniuxy.processdao.mapper.ValidCardStepMapper;
import com.woniuxy.processdao.model.ValidCardStep;
import com.woniuxy.processmodel.dto.ValidCardStepDto;
import com.woniuxy.processmodel.form.VenueLookValidCardStepForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.ValidCardStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 有效期会员卡流程 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class ValidCardStepServiceImpl extends ServiceImpl<ValidCardStepMapper, ValidCardStep> implements ValidCardStepService {
    @Autowired
    private ValidCardStepMapper validCardStepMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Resource
    private CardFeignClient cardFeignClient;
    @Override
    public void addValidCardStep(String orderNum) {
        QueryWrapper<ValidCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        ValidCardStep validCardStep = validCardStepMapper.selectOne(wrapper);
        if(!BeanUtil.isEmpty(validCardStep)){
            throw new ProcessException(5001,"该有效期卡订单已存在进度");
        }
        OrderDto orderDto = orderFeignClient.findByOrderNum(orderNum).getData();
        if(BeanUtil.isEmpty(orderDto)){
            throw new ProcessException(5009,"该订单不存在");
        }
        ValidCardDto validCardDto = cardFeignClient.findValidCardById(orderDto.getProductId()).getData();
        ValidCardStep step = new ValidCardStep();
        step.setOrderNum(orderNum);
        step.setValidCardId(orderDto.getProductId());
        step.setValidCardName(validCardDto.getCardName());
        step.setUserId(orderDto.getUserId());
        step.setVenueId(orderDto.getVenueId());
        step.setStartTime(orderDto.getOrderCreateTime());
        String startTime = orderDto.getOrderCreateTime();
        Integer validDate = validCardDto.getValidDate();
        String endTime = "";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = dateFormat.parse(startTime);
            // 使用 Calendar 计算 validDate 天后的日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.DATE, validDate);
            // 将 Calendar 转换为 Date 对象
            Date endDate = calendar.getTime();
            // 使用 SimpleDateFormat 将 endDate 格式化为字符串
            endTime = dateFormat.format(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        step.setEndTime(endTime);
        validCardStepMapper.insert(step);
    }

    @Override
    public ValidCardStep lookValidCardStep(Integer stepId) {
        ValidCardStep validCardStep = validCardStepMapper.selectById(stepId);
        return validCardStep;
    }

    @Override
    public ValidCardStep lookValidCardStepByOrderNum(Integer orderNum) {
        QueryWrapper<ValidCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        ValidCardStep validCardStep = validCardStepMapper.selectOne(wrapper);
        return validCardStep;
    }

    @Override
    public List<ValidCardStepDto> userLookValidCardStep(Integer userId) {
        QueryWrapper<ValidCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<ValidCardStep> validCardSteps = validCardStepMapper.selectList(wrapper);
        List<ValidCardStepDto> dtos = BeanUtil.copyToList(validCardSteps, ValidCardStepDto.class);
        return dtos;
    }

    @Override
    public PageInfo<ValidCardStepDto> venueLookValidCardStep(VenueLookValidCardStepForm form, Integer venueId) {
        QueryWrapper<ValidCardStep> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(form.getOrderNum()),"order_num",form.getOrderNum());
        wrapper.eq(!BeanUtil.isEmpty(form.getValidCardName()),"valid_card_name",form.getValidCardName());
        wrapper.eq("venue_id",venueId);
        Page<ValidCardStep> page = new Page<>(form.getPageNum(), form.getPageSize());
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(BeanUtil.copyToList(page.getRecords(),ValidCardStepDto.class));
        return pageInfo;
    }
}
